<?php

namespace App\Services\Api;

use App\Models\ClassBase;
use App\Models\ClassBaseInfo;
use App\Models\ClassBaseTime;
use App\Models\StoreRegion;
use App\Models\Store;
use App\Models\UserSubscribe;

class ClassService
{
    //首页课程列表
    public static function class_home_lists($param = [])
    {
        if(!empty($param['store_id'])){
            $store_id = explode(';',$param['store_id']);
        }

        $class = (new ClassBase());
        if(isset($store_id))$class = $class->whereIn('store_id', $store_id);
        $class = $class->select('class_name', 'id', 'class_price', 'class_tab', 'class_cove', 'class_name')->paginate(6);

        return $class;
    }

    //课程详情
    public static function class_home_info($param = [])
    {
        return (new ClassBase())->where('id',$param['class_id'])->select('class_cove', 'class_name', 'created_at' ,'updated_at' , 'class_tab', 'class_clothing', 'class_faq', 'class_note', 'class_introduction', 'class_img','class_price')->first();
    }

    //课程
    public static function class_base_info($param = [])
    {

        if(!empty($param['class_base_id']))$class_base_id = explode(';',$param['class_base_id']);

        if(!empty($param['store_id']))$store_id = explode(';',$param['store_id']);

        if(!empty($param['time_id']))$time_id = explode(';',$param['time_id']);

        if(!empty($param['class_week']))$class_week = explode(';',$param['class_week']);

        if(!empty($param['data_time'])){
            $start_time = date('Y-m-d 00:00:00', strtotime("{$param['data_time']} 00:00:00"));
            $end_time = date('Y-m-d 23:59:59', strtotime("{$param['data_time']} 23:59:59"));
        }else{
            $start_time = date('Y-m-d 00:00:00',time());
            $end_time = date('Y-m-d 23:59:59',time());
        }

        $class_base_info = ClassBaseInfo::with(['class_base', 'teacher', 'classsroom', 'store']);
        if(!empty($class_base_id))$class_base_info = $class_base_info->whereIn('class_base_id',$class_base_id);
        if(!empty($store_id))$class_base_info = $class_base_info->whereIn('store_id',$store_id);
        if(!empty($time_id))$class_base_info = $class_base_info->whereIn('class_time_id',$time_id);
        if(!empty($class_week))$class_base_info = $class_base_info->whereIn('class_week',$class_week);
        $class_base_info = $class_base_info->orderBy('class_base_info.class_time', 'desc')->get();

        $result = [];

        //查询预约数
        $class_base_info_id = array_column($class_base_info->toArray(),'id');

        $class_subscribe_number = (new UserSubscribe)->ClassSubscribeNumber($class_base_info_id, $start_time, $end_time);

        foreach ($class_base_info as $v) {
            $input['teacher']['head'] = $v->teacher->teacher_head;
            $input['teacher']['teacher_id'] = $v->teacher->id;
            $input['teacher']['name'] = $v->teacher->teacher_name;
            $input['teacher']['tab'] = $v->teacher->teacher_name;
            $input['class_info']['name'] = $v->class_base->class_name;
            $input['class_info']['class_id'] = $v->class_base->id;
            $input['class_info']['class_tab'] = $v->class_base->class_tab;
            $input['class_info']['class_time'] = $v->class_time;
            $input['class_info']['class_duration'] = $v->class_duration;
            $input['class_info']['class_week'] = $v->class_week;
            $input['class_info']['class_number_people'] = $v->class_number_people;
            $input['class_info']['class_subscribe_number'] = $class_subscribe_number[$v->id]??0;
            $input['class_classsroom']['numbering'] = $v->classsroom->numbering;
            $input['class_classsroom']['class_img'] = $v->classsroom->class_img;
            $input['class_classsroom']['image'] = $v->classsroom->image;
            $input['class_info_id'] = $v->id;

            $result[$v->store_id]['lists'][] = $input;
            $result[$v->store_id]['store_name'] = $v->store->store_name;

        }

        return array_values($result);

    }

    //条件查询
    public static function where($where_type = '')
    {
        switch ($where_type) {
            case 'class_lists':
                //课程列表
                return self::class_lists();
                break;
            case 'store_lists':
                //门店列表
                return self::store_lists();
                break;
            case 'time_lists':
                //时间段列表
                return self::time_lists();
                break;
        }
    }

    //课程列表
    public static function class_lists()
    {
        return (new ClassBase())->where('class_popular', '1')->select('class_name', 'id')->get();
    }

    //门店
    public static function store_lists()
    {

        $store_region = (new Store())->all();

        foreach ($store_region as $v) {
            $input['name'] = $v->store_name;
            $input['id'] = $v->id;
            $store_region_key[$v->city . '-' . $v->area][] = $input;
            unset($input);
        }

        //门店地址
        $store_region = (new StoreRegion())->all();

        foreach ($store_region as $v) {
            $city_result[$v->city][] = $v->area;
        }

        $result = [];

        foreach ($city_result as $k => $v) {

            $input = [];
            $input['city'] = $k;

            foreach ($city_result[$k] as $vv) {

                $region = [];
                $region['area_name'] = $vv;
                $region['store_lists'] = $store_region_key[$k . '-' . $vv] ?? [];

                $input['region'][] = $region;

            }

            $result[] = $input;

        }

        return $result;
    }

    //时段
    public static function time_lists()
    {

        $result = (new ClassBaseTime())->all();

        foreach ($result as &$v){

            $v->time_name = "{$v->time_start}~{$v->time_end}";

            unset($v->time_start);
            unset($v->time_end);

        }

        return $result;
    }
}
